package cc.linker.steplink.crm.merchant.controller;


import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.commons.utils.NumberUtils;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.service.ShopSetService;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 小B后台
 */
@RestController
@RequestMapping("/xb/set/")
public class XbShopSetController {

    @Autowired
    private ShopSetService shopSetService;
    @Autowired
    private UserInfoUtil userInfoUtil;


    /**
     * 获取店铺信息
     * @return
     */
    @GetMapping("/getshopinfo")
    public Map<String, Object> getShopInfo(HttpServletRequest request) throws Exception
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return shopSetService.getShopInfo(userinfo.getAdminId());
    }

    /**
     * 设置店铺信息
     * @return
     */
    @PostMapping("/setshopinfo")
    public void setShopInfo(
            HttpServletRequest request,
            @RequestBody Map<String,Object> params
    ) throws Exception
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        params.put("adminId", userinfo.getAdminId());
        if(!params.containsKey("businessName") || params.get("businessName").toString()==null || params.get("businessName").toString().length()<=0){
            throw new GlobalException(new ResponseException(400, "参数错误或者为空"));
        }
        Integer i = shopSetService.setShopInfo(params);
        if(i<1){
            throw new GlobalException(new ResponseException(400, "保存失败"));
        }
    }

    /**
     * 提交认证信息
     * @return
     */
    @PostMapping("/addverify")
    public void addVerify(
            HttpServletRequest request,
            @RequestBody Map<String,Object> params
    ) throws Exception
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        params.put("adminId", userinfo.getAdminId());
        //cardType
        if(!params.containsKey("cardType") || params.get("cardType").toString()==null || params.get("cardType").toString().length()<=0){
            throw new GlobalException(new ResponseException(400, "证件类型参数错误或者为空"));
        }
        //name
        if(!params.containsKey("name") || params.get("name").toString()==null || params.get("name").toString().length()<=0){
            throw new GlobalException(new ResponseException(400, "企业名称参数错误或者为空"));
        }
        //address
        if(!params.containsKey("address") || params.get("address").toString()==null || params.get("address").toString().length()<=0){
            throw new GlobalException(new ResponseException(400, "注册地址参数错误或者为空"));
        }
        //businessScope
        if(!params.containsKey("businessScope") || params.get("businessScope").toString()==null || params.get("businessScope").toString().length()<=0){
            throw new GlobalException(new ResponseException(400, "经营范围参数错误或者为空"));
        }
        shopSetService.addVerify(params);
    }

    /**
     * 获取钱包信息
     * @return
     */
    @GetMapping("/getwallet")
    public Map<String, Object> getWallet(HttpServletRequest request) throws Exception
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String adminId = userinfo.getAdminId();
        Map<String,Object> record = shopSetService.getWallet(adminId);
        Long customerBalance =(Long) record.getOrDefault("customerBalance",0L);
        Long customerFreeze =(Long) record.getOrDefault("customerFreeze",0L);
        Long customerTotalBalance = customerBalance + customerFreeze;
        record.put("customerTotalBalance", NumberUtils.fenToYuan(customerTotalBalance));
        record.put("customerBalance",NumberUtils.fenToYuan(customerBalance));
        record.put("customerFreeze",NumberUtils.fenToYuan(customerFreeze));
        return record;
    }
}
